package com.dundun.controller;
import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dundun.annotation.AuthCheck;
import com.dundun.common.utils.Ajax;
import com.dundun.domain.Internship;
import com.dundun.domain.JobRelationship;
import com.dundun.domain.Resume;
import com.dundun.service.InternshipService;
import com.dundun.service.JobRelationshipService;
import com.dundun.service.ResumeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Api(tags = "求职关系相关接口")
@RequestMapping("/")
@RestController
public class JobRelationshipController {

    @Resource
    private JobRelationshipService jobRelationshipService;

    @Resource
    private ResumeService resumeService;

    @Resource
    private InternshipService internshipService;
    @ApiOperation(value = "用户向岗位投递简历")
    @PostMapping("/PutNewTry")
    @AuthCheck
    public Ajax PutNewTry(Long internshipId, Long userId,Long resumeId) {
        System.out.println("internship:"+internshipId);
        System.out.println("user:"+userId);
        System.out.println("resume:"+resumeId);
        JobRelationship jobRelationship = new JobRelationship();
        jobRelationship.setUserId(userId);
        jobRelationship.setResumeId(resumeId);
        jobRelationship.setInternshipId(internshipId);
        jobRelationship.setDeliveryTime(new Date());
        jobRelationship.setUpdateTime(new Date());
        jobRelationship.setState(0);
        jobRelationship.setFitable(0);
        if (jobRelationshipService.save(jobRelationship)) {
            return Ajax.success();
        }
        return Ajax.error("投递失败,请重试");
    }

    @ApiOperation(value = "获取已投递简历状态")
    @GetMapping("/GetResumeStatus")
    @AuthCheck
    public Ajax getResumeStatus(int userId) {
        List<JobRelationship> jobRelationShipList = jobRelationshipService.list(
                new QueryWrapper<JobRelationship>().eq("user_id",userId)
        );
        List<Map<String,Object>> maps = new ArrayList<>();
        for (JobRelationship jobRelationship : jobRelationShipList) {
            Internship internship = internshipService.getById(jobRelationship.getInternshipId());
            Map<String,Object> map = new HashMap<>();
            map.put("jobStatus",jobRelationship);
            map.put("internship",internship);
            maps.add(map);
        }
        return Ajax.success().put("data",maps);
    }

    @ApiOperation(value = "分页获取已投递简历状态")
    @GetMapping("/GetResumeStatusByPage")
    public Ajax GetResumeStatusByPage(int userId,int pageNum,int pageSize) {
        List<Map<String,Object>> maps = jobRelationshipService.GetResumeStatusByPage(userId,pageNum,pageSize);
        return Ajax.success().put("data", maps);
    }

    @ApiOperation(value = "获取已投递简历列表")
    @GetMapping("/GetRecruitDetail")
    @AuthCheck
    public Ajax GetRecruitDetail(int internshipId) {
        List<JobRelationship> jobRelationShipList = jobRelationshipService.list(
                new QueryWrapper<JobRelationship>().eq("internship_id", internshipId)
        );
        List<Map<String,Object>> maps = new ArrayList<>();
        for (JobRelationship jobRelationship : jobRelationShipList) {
            Resume resume = resumeService.getById(jobRelationship.getResumeId());
            Map<String,Object> map = new HashMap<>();
            map.put("jobStatus",jobRelationship);
            map.put("resume",resume);
            maps.add(map);
        }
        return Ajax.success().put("data",maps);
    }

    @ApiOperation(value = "分页获取已投递简历列表")
    @GetMapping("/GetRecruitDetailByPage")
    public Ajax GetRecruitDetailByPage(int internshipId) {
        return Ajax.success().put("data", "the return data...");
    }

    @ApiOperation(value = "判断用户是否已投递该岗位")
    @GetMapping("/GetDelivered")
    @AuthCheck
    public Ajax GetDelivered(int userId,int internshipId) {
        Map<String , Object> param = new HashMap<>();
        param.put("user_id" , userId);
        param.put("internship_id" , internshipId);
        List<JobRelationship> jobRelationShipList = jobRelationshipService.list(
                new QueryWrapper<JobRelationship>().allEq(param)
        );
        System.out.println(jobRelationShipList.size());
        if (jobRelationShipList.size() > 0){
            return Ajax.success().put("hasDelivered",true);
        }
        return Ajax.success().put("hasDelivered",false);
    }

    @ApiOperation(value = "设置求职关系的已读状态")
    @GetMapping("/JobReadStatus")
    @AuthCheck
    public Ajax JobReadStatus(int jobRelationShipId,int status) {
        JobRelationship jobRelationship = jobRelationshipService.getById(jobRelationShipId);
        jobRelationship.setState(status);
        if (jobRelationshipService.updateById(jobRelationship)){
            return Ajax.success();
        }else {
            return Ajax.error();
        }
    }

    @ApiOperation(value = "设置求职关系的合适状态")
    @GetMapping("/JobFitStatus")
    @AuthCheck
    public Ajax JobFitStatus(int jobRelationShipId,int status) {
        JobRelationship jobRelationship = jobRelationshipService.getById(jobRelationShipId);
        jobRelationship.setFitable(status);
        if (jobRelationshipService.updateById(jobRelationship)){
            return Ajax.success();
        }else {
            return Ajax.error();
        }
    }
}
